// pages/modifyaddress/modifyaddress.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
      navtop:"",
      region:[null,null,null],
      itemId:0,
      ziliao:{        
  
      },
    },
    onLoad(options) {
       // 导航栏
      // 1.获取屏幕可使用宽度
      let windowWidth = wx.getSystemInfoSync().windowWidth;
      // 2.获取状态栏高度
      const statusBarHeight = wx.getSystemInfoSync().statusBarHeight;
      //3.获取胶囊按钮位置信息
      const menuButton = wx.getMenuButtonBoundingClientRect();
    
      this.setData({
      navtop:menuButton.top,
        ziliao:options,
        region:[options.province,options.city,options.area]
      })
  
      console.log(this.data.ziliao);
      
    },
    gitfor(){
      wx.navigateBack({
        delta: -1
      })
    },
    bindRegionChange: function (e) {
      this.setData({
        region: e.detail.value
      })
    },
    formSubmit: function(e) {
      const id1 = this.data.ziliao.id;
      console.log(id1);
      const that = this;
      this.setData({
        ziliao: e.detail.value,
      }, function() {
          // 在 setData 回调函数中执行 setziliao 方法
          that.setziliao(id1);
      });
      console.log(this.data.ziliao);
  },
  containsDigitOrSymbol(str) {
    // 定义正则表达式，匹配任意一个数字或者标点符号
    var regex = /[0-9\p{P}\p{S}]/u;
    // 使用test方法检测字符串是否匹配正则表达式
    return regex.test(str);
  },
    setziliao(id){
      const that=this
      const app =getApp();
      const phoneRegex = /^1[3-9]\d{9}$/;
      const regexSpecialChars = /[!@#$%^&*(),.?":{}|<>]/;
      console.log(that.data.ziliao);
      if(that.data.ziliao.xm&&that.data.ziliao.sjh&&that.data.ziliao.telephone&&that.data.region[0]&&that.data.region[1]&&that.data.region[2]&&that.data.ziliao.addr ==null){
        wx.showToast({
          title: "请正确输入信息",
          icon: 'none'
        });
      }else if(!phoneRegex.test(that.data.ziliao.sjh)){
        wx.showToast({
          title: "请输入正确的电话号码",
          icon: 'none'
        });
      }
      else if(that.containsDigitOrSymbol(that.data.ziliao.xm)==true){
        wx.showToast({
          title: "请输入正确的姓名",
          icon: 'none'
        });
      }else if(regexSpecialChars.test(this.data.ziliao.address)){
        wx.showToast({
            title: "请输入正确的地址",
            icon: 'none'
          });
    }else{
      wx.request({
        url: 'https://dj.ifankui.com/api/usrEn/editDeliveryAddress',
        method:'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data:{
          openid:app.globalData.openid,
          addresId:id,
          xm:that.data.ziliao.xm,
          sjh:that.data.ziliao.sjh,
          telphone:that.data.ziliao.telphone,
          province:that.data.region[0],
          city:that.data.region[1],
          area:that.data.region[2],
          addr:that.data.ziliao.address,
        },
        success(res){
          console.log(res)
          this.gitfor()
        }
      })}
    }
  })